package me.jiatao.usermanage.controller;

import me.jiatao.usermanage.pojo.User;
import me.jiatao.usermanage.service.UserService;
import me.jiatao.usermanage.view.DataGridPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/userlist")
    public ModelAndView showUserList() {
        ModelAndView userListView = new ModelAndView("users");
        return userListView;
    }

    /**
     * 通用的跳转页面
     *
     * @param pageName
     * @return
     */
    @RequestMapping(value = "page/{pageName}")
    public String toPage(@PathVariable("pageName") String pageName) {
        return pageName;
    }

    /**
     * 输出用户列表
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public DataGridPage getUserList(@RequestParam("page") int page, @RequestParam("rows") int rows) {
        DataGridPage gridPage = userService.getPage(page, rows);
        return gridPage;
    }

    @RequestMapping("/save")
    @ResponseBody
    public HashMap<String, Integer> save(User user) {
        HashMap<String, Integer> resData = new HashMap<>();

        int rows = userService.saveUser(user);
        if (rows > 0) {
            resData.put("status", 200);
        } else {
            resData.put("status", 400);
        }

        return resData;
    }


    @RequestMapping("/modify-view")
    @ResponseBody
    public ModelAndView modifyView(@RequestParam("id") Long ids) {
        User user = userService.getUserByID(ids);

        user.toString();

        ModelAndView modifyView;
        if (user == null) {
            modifyView = new ModelAndView("user-add");
        } else {
            modifyView = new ModelAndView("user-modify");
            modifyView.addObject("user", user);
        }

        return modifyView;
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public HashMap<String, Integer> modify(User user) {
        HashMap<String, Integer> resData = new HashMap<>();
        int resRows = userService.modifyUser(user);
        if (resRows >= 1) {
            resData.put("status", 200);
        } else {
            resData.put("status", 400);
        }
        return resData;
    }

    /**
     * 删除数据
     *
     * @param userID
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public HashMap<String, Integer> delete(@RequestParam("id") Long userID) {
        HashMap<String, Integer> resData = new HashMap<>();
        int resRows = userService.deleteUser(userID);
        if (resRows >= 1) {
            resData.put("status", 200);
        } else {
            resData.put("status", 400);
        }
        return resData;
    }

    @RequestMapping(value="/export/excel")
    public ModelAndView exportExcel(@RequestParam("page") Integer pageNum , @RequestParam("rows") Integer pageSize){

        //查询要导出excel的内容

        DataGridPage page = userService.getPage(pageNum , pageSize);
        List<User> users = (List<User>) page.getRows();//users就是要导出的数据

        ModelAndView mv = new ModelAndView("userExcel");
        mv.addObject("users", users); //想model中去添加数据库
        return mv;
    }
}
